classSolution(object):definorderTraversal(self, root):#递归""":type root: TreeNode :rtype: List[int]"""ifnotroot:return[]returnself.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) Java解法 classSolution {publicList < Integer >inorderTraversal(TreeNode root) ...
叫 Morris Traversal,在介绍这种方法之前,先来引入一种新型树,叫Threaded binary tree,这个还不太好翻译,第一眼看上去以为是叫线程二叉树,但是感觉好像又跟线程没啥关系,后来看到网上有人翻译为螺纹二叉树,但博主认为这翻译也不太敢直视,很容易让人联想到为计划生育做出突出贡献的某世界...
2.2 last.right 不为 null,说明之前已经访问过,第二次来到这里,表明当前子树遍历完成,保存 cur 的值,更新 cur = cur.right public List<Integer> inorderTraversal3(TreeNode root) { List<Integer> ans = new ArrayList<>(); TreeNode cur = root; while (cur != null) { //情况 1 if (cur.left ...
vector<int> inorderTraversal(TreeNode* root) { vector<int> res; stack<TreeNode*> treeroot; while(root!=NULL||!treeroot.empty()) { while(root!=NULL) { treeroot.push(root); root=root->left; } root = (); treeroot.pop(); res.push_back(root->val); root = root->right; } retu...
Given a binary tree, return the inorder traversal of its nodes’ values. For example: Given binary tree [1,null,2,3], \ 1. return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 思路: 1、题意为中序遍历二叉树。遍历顺序为左—>根—>右。
Given a binary tree, return the inorder traversal of its nodes' values. 给定一个二叉树,返回中序遍历后所有节点的值。 Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Follow up: Recursive solution is trivial, could you do it iteratively? 追加问题:可以不用递归,而是用遍历实...
Binary Tree Level Order Traversal 二叉树层序遍历 Example Givenbinary tree[3,9,20,null,null,15,7],3/\920/\157returnits level order traversalas:[[3],[9,20],[15,7]] BFS方法 var levelOrder=function(root){if(!root)return[]conststack=[root]constres=[]while(stack.length){constlen=stack...
名字叫做, morrois traversal, 自己写了下: My code: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{publicList<Integer>inorderTraversal(TreeNoderoot){List...
Binary tree traversal: Preorder, Inorder, and Postorder In order to illustrate few of the binary tree traversals, let us consider the below binary tree: Preorder traversal: To traverse a binary tree in Preorder, following operations are carried-out (i) Visit the root, (ii) Traverse the le...
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 做了第106题再来做这个就简单了,写递归的话思路一模一样: /** * Definition for a binary tree node. ...