Data structure tree traversal

Traversal introduction

1. First-order traversal

Recursion:

class Solution {
    
    
    public List<Integer> postorderTraversal(TreeNode root) {
    
    
        List<Integer> list = new ArrayList<>();
        if(root == null){
    
    
            return list;
        }
        help(root,list);
        return list;
    }
    public void help(TreeNode root,List<Integer> list){
    
    
        if(root == null){
    
    
            return ;
        }
        list.add(root.val);
        help(root.left,list);
        help(root.right,list); 
    }
}

Iteration:

public List<Integer> preOrderIteration(TreeNode head) {
    
    
	if (head == null) {
    
    
		return;
	}
	Stack<TreeNode> stack = new Stack<>();
    List<Integer> ret = new ArrayList<Integer>();
	stack.push(head);
	while (!stack.isEmpty()) {
    
    
		TreeNode node = stack.pop();
		ret.add(node.val);
		if (node.right != null) {
    
    
			stack.push(node.right);
		}
		if (node.left != null) {
    
    
			stack.push(node.left);
		}
	}
   return ret;
}

2. In-order traversal

The way the stack is implemented:

public class Solution {
    
    
    public List < Integer > inorderTraversal(TreeNode root) {
    
    
        List < Integer > res = new ArrayList <> ();
        Stack < TreeNode > stack = new Stack <> ();
        TreeNode curr = root;
        while (curr != null || !stack.isEmpty()) {
    
    
            while (curr != null) {
    
    
                stack.push(curr);
                curr = curr.left;
            }
            curr = stack.pop();
            res.add(curr.val);
            curr = curr.right;
        }
        return res;
    }
}

Recursive way:

class Solution {
    
    
    private List<Integer> list = new ArrayList<>();
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        if(root != null){
    
    
            inorderTraversal(root.left);
            list.add(root.val);
            inorderTraversal(root.right);
        }
        return list;
    }
}

public List<Integer> help(TreeNode root,List<Integer> list){
    
    
        if(root == null){
    
    
            return list;
        }
        list = help(root.left,list);
        list.add(root.val);
        list = help(root.right,list);
        return list;
    }

3. Post-order traversal

Recursion:

class Solution {
    
    
    public List<Integer> postorderTraversal(TreeNode root) {
    
    
        List<Integer> list = new ArrayList<>();
        if(root == null){
    
    
            return list;
        }
        help(root,list);
        return list;
    }
    public void help(TreeNode root,List<Integer> list){
    
    
        if(root == null){
    
    
            return ;
        }
        help(root.left,list);
        help(root.right,list);
        list.add(root.val);
    }
}

Iteration:
first convert to center right and left, and then use the characteristics of the stack to convert to left and right center

public static void postOrderIteration(TreeNode head) {
    
    
		if (head == null) {
    
    
			return;
		}
		Stack<TreeNode> stack1 = new Stack<>();
		Stack<TreeNode> stack2 = new Stack<>();
		stack1.push(head);
		while (!stack1.isEmpty()) {
    
    
			TreeNode node = stack1.pop();
			stack2.push(node);
			if (node.left != null) {
    
    
				stack1.push(node.left);
			}
			if (node.right != null) {
    
    
				stack1.push(node.right);
			}
		}
		while (!stack2.isEmpty()) {
    
    
			System.out.print(stack2.pop().value + " ");
		}
	}

Direct post-order traversal

 public List<Integer> postorderTraversal(TreeNode root) {
    
    
        List<Integer> ans = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode current = root, r = null;  //r结点用来区分之前的结点是否被访问过
        while(current != null || !stack.isEmpty()){
    
    
            while(current != null){
    
    
                stack.push(current);
                current = current.left;
            }
            current = stack.peek();    //看最左结点有没有右子树
            if(current.right != null && current.right != r){
    
    
                current = current.right;
            }else{
    
    
                current = stack.pop();    //访问该结点,并标记被访问
                ans.add(current.val);
                r = current;
                current = null;
            }
        }
        return ans;
}

Guess you like

Origin blog.csdn.net/weixin_42272869/article/details/112260724
Recommended