[Java] summary traverse binary tree (recursive and non-recursive)

Preamble binary tree traversal

Recursion:

public ArrayList<Integer> postorder(TreeNode root) {
       ArrayList<Integer> res = new ArrayList<Integer>();
       //采用递归方式
      if (root==null)
          return res;
      if (root!=null)
         res.add(root.val);
      if (root.left!=null)
      postorderTraversal(root.left);
      if (root.right!=null)
      postorderTraversal(root.right);
      return res;
}

Non-recursive:

  The ArrayList <Integer> = RES new new the ArrayList <Integer> (); 
    public the ArrayList <Integer> preorderTraversal (the TreeNode the root) { 
        // non-recursive way: use the stack into the stack to the right subtree, the left subtree into the stack 
        Stack <the TreeNode> = new new Stack Stack <the TreeNode> (); 
        IF (the root == null) 
            return RES; 
        IF (= the root null!) { 
            stack.push (the root); 
            the while (stack.isEmpty ()!) { 
                the TreeNode n- stack.pop = (); 
                res.add (n.val); 
                IF (n.right = null!) 
                    stack.push (n.right); 
                IF (! = null n.left) 
                    stack.push (n.left ); 
            } 
        }  
        return RES;
        }
    }

 In order binary tree traversal

Recursion:

public ArrayList<Integer>  inorder(TreeNode root) {
       ArrayList<Integer> res = new ArrayList<Integer>();
       //采用递归方式
      if (root==null)
          return res;
      if (root.left!=null)
      postorderTraversal(root.left);
      res.add(root.val);
      if (root.right!=null)
      postorderTraversal(root.right);
      return res;
}

Non-recursive:

public ArrayList<Integer> inorder(TreeNode root) {
       ArrayList<Integer> res = new ArrayList<>();
        if(root == null) {
             return res;
         }
       Stack<TreeNode> stack = new Stack<>();
       TreeNode p = root;
        while(p != null || !stack.isEmpty()) {
             if(p != null) {
              stack.push(p);
                 p = p.left;
          } else {
                 p = stack.pop();
                res.add(p.val);
                 p = p.right;
             }
         }
          return res;
    }

After the binary tree traversal sequence

Recursion:

public ArrayList<Integer>  postorder(TreeNode root) {
       ArrayList<Integer> res = new ArrayList<Integer>();
       //采用递归方式
      if (root==null)
          return res;
      if (root.left!=null)
      postorderTraversal(root.left);
      if (root.right!=null)
      postorderTraversal(root.right);
      res.add(root.val);
      return res;
}

Non-recursive:

//! ! ! Do not forget to guide the corresponding packet 
public the ArrayList <Integer> postorder (the TreeNode the root) { 
       the ArrayList <Integer> = RES new new the ArrayList <Integer> (); 
      // a non-recursive manner: 
        IF (the root == null) 
           return RES; 
        Stack <the TreeNode > = new new Stack Stack <the TreeNode> (); 
        the TreeNode P = the root; 
        the TreeNode null = R & lt; 
        the while (!! = null || stack.isEmpty P ()) { 
            IF (! P = null) { 
                stack.push (P ); 
                P = p.left; // stack all left child until the child is left empty 
            } 
            the else { 
                P = stack.peek ();   
                P = p.right; // Ruoguo left child node does not access the stack top right child elements 
                if (p! = null && p ! = r) {
                    stack.push (p); // Ruoguo a right child, let all of its left child extended stack 
                    P = p.left; 
                } the else { 
                    P = stack.pop (); // Ruoguo no right child, put on the stack, access, 
                    res.add (p.val); 
                    R & lt = p; // R & lt recording just visited node 
                    p = null; // the p blanking, can continue to access the top element 
                } 
            } 
        } 
        RES return; 
    } 
}

 

Guess you like

Origin www.cnblogs.com/yaogungeduo/p/11245116.html