public List<Integer> inorderTraversal(TreeNode root) { /** * 中序遍历,先左子树,再根,最后右子树 */ if(root == null) return list; if(root.left != null){ inorderTraversal(root.left); } list.add(root.val); if(root.right != null){ inorderTraversal(root.right); } return list; }...
【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历) 这道题是LeetCode里的第94道题。 题目要求: 给定一个二叉树,返回它的中序遍历。 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶:递归算法很简单,你可以通过迭代算法完成吗? 解题代码: /** * Definition for a binary t...
Leetcode 94: Binary Tree Inorder Traversal-中序遍历 自动驾驶搬砖师 一步一个台阶,一天一个脚印一、题目描述 给定一个二叉树,返回它的中序遍历。 二、解题思路 2.1 递归法 时间复杂度 O(n) 空间复杂度 O(n) 2.2 迭代法 时间复杂度 O(n) 空间复杂度 O(n)(...
链接:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/ 难度:Medium 题目: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 duplicat...
名字叫做, 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...
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal,Givenpreorderandinordertraversalofatree,constructthebinarytree.recursiveanswer:递归方法:在preorder找到root节点,接着在inorder里找到ro
LeetCode-105. Construct Binary Tree from Preorder and Inorder Traversal,Givenpreorderandinordertraversalofatree,constructthebinarytree.Note:Youmayassumethatduplicatesdonotexistinthetree.Forexample,givenpreorder=[3,9,20,15,7]inorder=[9,3...
leetcodebinary-treeproblem-solvinginorder-traversal UpdatedFeb 6, 2020 Binary Tree Traversals and Views. binarytreetree-traversaltree-viewalgorithms-and-data-structuresbinarytree-javatree-traversal-algorithmlevel-order-traversalinorder-traversalpreorder-traversalpostorder-traversalvertical-order-traversalleft-view...
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…
class Solution { public: vector<int> result; void inorder(TreeNode *root) { if (root == NULL) return; inorder(root->left); result.push_back(root->val); inorder(root->right); } vector<int> inorderTraversal(TreeNode *root) { result.clear(); inorder(root); return result; } };...