主要有两种实现方式:递归和非递归。
遍历方式主要有:前序遍历、中序遍历、后序遍历和层序遍历。
#前序遍历 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { //前序遍历递归实现 List<Integer> ans; private void preOrder(TreeNode root) { if(root == null) return ; ans.add(root.val); preOrder(root.left); preOrder(root.right); } public List<Integer> preorderTraversal(TreeNode root) { ans = new LinkedList<>(); preOrder(root); return ans; } }
class Solution { //前序遍历非递归(用栈实现) public List<Integer> preorderTraversal(TreeNode root) { List<Integer> ans = new LinkedList<>(); Stack<TreeNode> st = new Stack<>(); TreeNode p = root; //根结点先不用入栈 while(p != null || !st.isEmpty()) { if(p != null) { ans.add(p.val); //访问完当前节点 st.push(p); //将当前节点入栈,为了可以访问右子树 p = p.left; } else { p = st.pop(); //将已访问的节点出栈 p = p.right; //向其右子树方向走 } } return ans; } }
//中序遍历 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { //中序遍历递归实现 List<Integer> ans; private void inOrder(TreeNode root) { if(root == null) return ; inOrder(root.left); ans.add(root.val); inOrder(root.right); } public List<Integer> inorderTraversal(TreeNode root) { ans = new LinkedList<>(); inOrder(root); return ans; } } class Solution { public List<Integer> inorderTraversal(TreeNode root) {//中序遍历非递归(用栈) List<Integer> ans = new LinkedList<>(); Stack<TreeNode> st = new Stack<>(); TreeNode p = root; //根结点先不用入栈 while(p != null || !st.isEmpty()) { if(p != null) { st.push(p); p = p.left; } else { p = st.pop(); ans.add(p.val); //和先序遍历的差别在于先序遍历先访问再入栈,中序遍历出栈再访问 p = p.right; } } return ans; } }
//层序遍历 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution {//层序遍历非递归(用队列),无递归方式 public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> ans = new LinkedList<>(); if(root == null) return ans; Queue<TreeNode> que = new LinkedList<>(); que.offer(root); while(!que.isEmpty()) { int len = que.size(); List<Integer> L = new LinkedList<>(); for(int i = 0; i < len; i++) { TreeNode t = que.poll(); L.add(t.val); if(t.left != null) que.offer(t.left); if(t.right != null) que.offer(t.right); } ans.add(new LinkedList(L)); } return ans; } }
//后序遍历 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution {//后序遍历递归 List<Integer> ans; private void postOrder(TreeNode root) { if(root == null) return ; postOrder(root.left); postOrder(root.right); ans.add(root.val); } public List<Integer> postorderTraversal(TreeNode root) { ans = new LinkedList<>(); postOrder(root); return ans; } } class Solution {//后序遍历非递归(用栈) public List<Integer> postorderTraversal(TreeNode root) { List<Integer> ans = new LinkedList<>(); Stack<TreeNode> st = new Stack<>(); if(root == null) return ans; st.push(root); while(!st.isEmpty()) { root = st.pop(); ans.add(root.val); if(root.left != null) st.push(root.left); if(root.right != null) st.push(root.right); } Collections.reverse(ans); return ans; } }