二叉树的遍历
来源:互联网 发布:手机照片合成软件 编辑:程序博客网 时间:2024/05/03 00:42
- 前序遍历 preorder:根左右
- 中序遍历 inorder:左根右
- 后序遍历 postorder:左右根
每一种遍历都有递归和循环两种方法,其中递归比循环简单得多。
三种遍历递归方法的区别就是递归函数中递归左孩子、右孩子与当前节点的顺序。
而循环则需借用栈来实现。
前序遍历
递归
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); helper(result,root); return result; } public void helper(List<Integer> list,TreeNode node){ if(node == null) return; //根左右 list.add(node.val); helper(list,node.left); helper(list,node.right); }}
循环
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); if(root == null) return result; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while(!stack.isEmpty()){ TreeNode node = stack.pop(); result.add(node.val); if(node.right!=null){ stack.push(node.right); } if(node.left!=null){ stack.push(node.left); } } return result; }}
中序遍历
递归
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); helper(result,root); return result; } public void helper(List<Integer> list,TreeNode node){ if(node == null) return; //左根右 helper(list,node.left); list.add(node.val); helper(list,node.right); }}
循环
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode node = root; while(!stack.isEmpty() || node != null){ while(node != null){ stack.push(node); node = node.left; } node = stack.pop(); result.add(node.val); node = node.right; } return result; }}
后序遍历
递归
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); helper(result,root); return result; } public void helper(List<Integer> list,TreeNode node){ if(node == null) return; //左右根 helper(list,node.left); helper(list,node.right); list.add(node.val); }}
循环
用prev表示上一次遍历到的节点,curr是当前遍历的节点,需根据二者的关系来确定接下来的遍历方向。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); if(root == null) return; TreeNode prev = null; TreeNode curr = root; stack.push(root); while(!stack.isEmpty()){ curr = stack.peek(); if(prev == null || prev.left == curr || prev.right == curr){ // traverse down the tree if(curr.left !=null){ stack.push(curr.left); }else if(curr.right != null){ stack.push(curr.right); } }else if(curr.left == prev){ // traverse up the tree from the left if(curr.right != null){ stack.push(curr.right); } }else{ // traverse up the tree from the right result.add(curr.val); stack.pop(); } prev = curr; } return result; }}
0 0
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 36匹马赛跑,选出前三名的次数
- HTTP的请求方法OPTIONS
- cpufreq 之conservative governe的实现
- python 读取apk信息
- OpenSceneGraph实现的NeHe OpenGL教程 - 第一课
- 二叉树的遍历
- Qt Creator的pro文件格式
- ubuntu 12.04不知道root密码,修改密码
- Button设置透明度和简单形状
- ASP.NET 身份验证机制
- jquery星级评分
- eclipse反编译插件安装
- beanutils 入门
- VS2015中运行ASPX老项目出错HTTP Error 500.23 - Internal Server Error错误