144-94-145 二叉树的前中后序遍历
来源:互联网 发布:淘宝怎样才能分期付款 编辑:程序博客网 时间:2024/05/29 17:44
前序遍历递归解法
public class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer>list =new ArrayList<>(); preorder(root,list); return list; } public void preorder(TreeNode root,List<Integer> list){ if(root==null) return ; if(root!=null){ list.add(root.val); } preorder(root.left,list); preorder(root.right,list); }}
先序遍历非递归解法
先把所有左节点的值放到stack中 同时把左节点的值放入list,此时已经把所有最左侧的节点放入list 然后逐个弹出 弹出时遍历它右节点(子树)
public class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); while( !stack.isEmpty() || root!=null){ while(root != null){ stack.push(root); list.add(root.val); root = root.left; } if( !stack.isEmpty()){ root = stack.pop(); // list.add(root.val); root = root.right; } } return list; } }
中序遍历的递归解法
public class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list=new ArrayList<>(); inorder(root,list); return list; } public void inorder(TreeNode root,List<Integer> list){ if(root==null) return ; if(root.left!=null) { inorder(root.left,list); } list.add(root.val); if(root.right!=null) inorder(root.right,list); }}
中序遍历的非递归解法
先把左右的左节点放到stack中,然后一个一个pop出来 pop的同时检查对应的右节点
public class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); while( !stack.isEmpty() || root!=null){ while(root != null){ stack.push(root); root = root.left; } if( !stack.isEmpty()){ // 当遇到节点为空时已经遍历完了此部分,需要再弹出一个节点进行遍历 root = stack.pop(); list.add(root.val); root = root.right; } } return list; } }
addfirst() 每次将节点插在第一的位置而不是插在末尾
public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> list=new ArrayList<>(); postorder(root,list); return list; } public void postorder(TreeNode root,List<Integer> list){ if(root==null) return; if(root.left!=null){ postorder(root.left,list); } if(root.right!=null){ postorder(root.right,list); } list.add(root.val); }}
后序遍历非递归解法
public class Solution { public List<Integer> postorderTraversal(TreeNode root) { LinkedList<Integer> list = new LinkedList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); while( !stack.isEmpty() || root!=null){ while(root != null){ stack.push(root); list.addFirst(root.val); root = root.right; } if( !stack.isEmpty()){ root = stack.pop(); root = root.left; } } return list; } }
0 0
- 144-94-145 二叉树的前中后序遍历
- 二叉树遍历LeetCode#144 #94 #145 (前中后序遍历)
- [Leetcode] 二叉树的遍历(#94,#144,#145 )
- 二叉树的前中后序遍历
- 二叉树的前中后序遍历
- 二叉树的前中后序遍历
- 二叉树的前中后序遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- CentOS7 搭建FTP服务器
- ListView优化-ViewHolder
- 1.1.5 计算机网络的标准化工作及相关组织
- LOAD_DLL_DEBUG_EVENT取DLL名称
- 《Linux/Unix系统编程手册》中的例子在Linux下运行的方法
- 144-94-145 二叉树的前中后序遍历
- 一次面试题:公交网络上(双向图)最优路线的笨解法
- Windows下git--安装
- caffe (1) 如何给图像批量打标签 及 转为lmdb
- JSP分页技术
- 元组的可变性
- C++:error C4996: 'std::_Uninitialized_copy0': Function call with parameters that may be unsafe
- ROS脚本开机自启动
- Chuck语言学习笔记——0.前言:我为什么要学习这门语言