[Leetcode] Binary tree travelsal (preorder, inorder, postorder)

一、前序

 1 public List<Node> preOrder(Node root){
 2     List<Node> res = new LinkedList<Node>();
 3     Stack<Node> stack = new Stack<Node>();
 4     stack.push(root);
 5     while(root!=null||!stack.isEmpty()){
 6         while(root){
 7             res.add(root);
 8             stack.push(root);
 9             root=root.left;
10         }
11         Node tmp = stack.pop();
12         root= tmp.right;
13     }
14     return res;
15 }

二、中序

 1 public List<Node> inOrder(Node root){
 2     List<Node> res = new LinkedList<Node>();
 3     Stack<Node> stack = new Stack<Node>();
 4     stack.push(root);
 5     while(root!=null||!stack.isEmpty()){
 6         while(root){
 7             stack.push(root);
 8             root= root.left;
 9         }
10         Node tmp = stack.pop();
11         res.add(tmp);
12         root=tmp.right;
13     }
14 }

三、后序

 1 public List<Node> postOrder(Node root){
 2     List<Node> res = new LinkedList<Node>();
 3     Stack<Node> stack = new Stack<Node>();
 4     stack.push(root);
 5     Node head = root;
 6     while(!stack.isEmpty()){
 7         Node current =stack.peek();//do not know if should pop()
 8         if(current.right==head||current.left==head||(current.left==null||current.right==null)){
 9             stack.pop();
10             res.add(current);
11             head=current;
12         }else{
13             if(current.right!=null) stack.push(current.right);
14             if(current.left!=null) stack.push(current.left);
15         }
16     }
17 }
时间: 2024-08-25 00:19:49

[Leetcode] Binary tree travelsal (preorder, inorder, postorder)的相关文章

889.Construct Binary Tree from Preorder and Postorder Traversal

Return any binary tree that matches the given preorder and postorder traversals. Values in the traversals pre and post are distinct positive integers. Example 1: Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7] Note: 1 <=

[LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树

Return any binary tree that matches the given preorder and postorder traversals. Values in the traversals?pre?and?post?are distinct?positive integers. Example 1: Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7] Note: 1 <=

(二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal

Return any binary tree that matches the given preorder and postorder traversals. Values in the traversals pre and post are distinct positive integers. Example 1: Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7] Note: 1 <=

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

[LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal

前中后遍历 递归版 1 /* Recursive solution */ 2 class Solution { 3 public: 4 vector<int> preorderTraversal(TreeNode *root) { 5 6 vector<int> result; 7 preorderTraversal(root, result); 8 9 return result; 10 } 11 12 void preorderTraversal(TreeNode *root,

[Leetcode][Tree][Construct Binary Tree from Preorder/Postorder and Inorder Traversal ]

从树的中序遍历+前/后序遍历重建一棵树. 必须使用iterator才能过,否则会MLE. 1.preorder + inorder 第一个版本,使用坐标范围: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}

LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal

一:LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal 题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 链接:https://leetcode.com/problems/co

[LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal

题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

36. Construct Binary Tree from Inorder and Postorder Traversal &amp;&amp; Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assu