root.left.right =newTreeNode(3); root.right =newTreeNode(6); root.right.left =newTreeNode(5); root.right.right =newTreeNode(7); root.right.right.right =newTreeNode(8); System.out.println(Main21.inorderTraversal(root)); } publicstaticclassTreeNode { intval; TreeNode left; TreeNode ...
* public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * }*/publicclassSolution {/***@paramroot: The root of binary tree. *@return: Inorder in ArrayList which contains node values.*/publicArrayList<Integer>inorderTraversal(TreeNode root) {//write y...
publicList<Integer>inorderTraversal(TreeNoderoot){List<Integer>ans=newArrayList<>();Stack<TreeNode>stack=newStack<>();TreeNodecur=root;while(cur!=null||!stack.isEmpty()){//节点不为空一直压栈while(cur!=null){stack.push(cur);cur=cur.left;//考虑左子树}//节点为空,就出栈cur=stack.pop()...
94. 二叉树的中序遍历 Binary Tree Inorder Traversal 力扣 LeetCode 题解 05:49 95. 不同的二叉搜索树 II Unique Binary Search Trees II 力扣 LeetCode 题解 04:30 96. 不同的二叉搜索树 Unique Binary Search Trees 力扣 LeetCode 题解 09:13 97. 交错字符串 Interleaving String 力扣 LeetCode ...
94. Binary Tree Inorder Traversal 题目描述 Given a binary tree, return theinorder For example: Given binary tree [1,null,2,3], 1 \ 2 / 3 return [1,3,2]. Note: 思路 本题的目的是将一个二叉树结构进行中序遍历输出...
7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> inorderTraversal(TreeNode *root) { 13 vector<int> res; 14 if(root==NULL) return res; 15 stack<TreeNode*> s; ...
* TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{public List<Integer>inorderTraversal(TreeNode root){List<Integer>result=newLinkedList<>();TreeNode current=root;TreeNode prev=null;while(current!=null){// left firstif(current.left==null){result.add(current.val);...
名字叫做, 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...
* Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> inorderTraversal(TreeNode *root) { ...