下面这种解法跟Binary Tree Preorder Traversal中的解法二几乎一样,就是把结点值加入结果 res 的步骤从 if 中移动到了 else 中,因为中序遍历的顺序是左-根-右,参见代码如下: 解法三: classSolution {public: vector<int> inorderTraversal(TreeNode*root) { vector<int>res; stack<TreeNode*>s; TreeNode*p ...
*/publicclassBinaryTreeInOrderTraversal{privateint[] result =null;intpos=0;publicint[] traversal(char[] tree) { result =newint[tree.length]; pos =0; traversalByRecursion(createTree(tree));returnresult; }publicint[] traversal1(char[] tree) { result =newint[tree.length]; pos =0; travers...
left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> ret; if(nullptr == root) return ret; inOrder(root, ret); return ret; } private: void inOrder(TreeNode* root, vector<int>& ret) { ...
1. Problem Descriptions:Given two integer arrays inorderandpostorderwhereinorderis the inorder traversal of a binary tree andpostorderis the postorder traversal of the same tree, construct and retu…
名字叫做, 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...
preorder 的范围 [begin, end] 并不是 inorder 的范围,如果直接拿begin, end 来作为inorder[] 数组的边界条件,就会出错。所以必须要分为两个范围。 [preorderBegin, preorderEnd] [inorderBegin, inorderEnd] 这两个范围。 第二个错误,错的更深,其实到最后都没有彻底意识到,一定要当心,做此类题时。
102. 二叉树的层序遍历 - 给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。 示例 1: [https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg] 输入:root = [3,9,20,null,null,15,7] 输出:[[3],[9,20],[15,7]]
leetcode 107. Binary Tree Level Order Traversal II Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree[3,9,20,null,null,15,7],...
Given therootof a binary tree, returnthe vertical order traversal of its nodes' values. (i.e., from top to bottom, column by column). If two nodes are in the same row and column, the order should be from left to right. Example 1: ...
vector<int> inorderTraversal(TreeNode *root) { vector<int> rs; if (!root) return rs; stack<TreeNode *> stk; stk.push(root); bool flag = false; while (!stk.empty()) { if (!flag) while (stk.top()->left) stk.push(stk.top()->left); ...