- 浏览: 185679 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
中序遍历一棵树,我们可以采用递归,也可以用迭代,用迭代的时候借助堆栈来完成。
1,递归
2,迭代
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
中序遍历一棵树,我们可以采用递归,也可以用迭代,用迭代的时候借助堆栈来完成。
1,递归
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<Integer>(); if(root == null) return list; getIT(root, list); return list; } public void getIT(TreeNode root, List<Integer> list) { if(root == null) return; getIT(root.left, list); list.add(root.val); getIT(root.right, list); } }
2,迭代
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); while(root != null || !stack.isEmpty()) { if(root != null) { stack.push(root); root = root.left; } else { TreeNode node = stack.pop(); list.add(node.val); root = node.right; } } return list; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 271Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 275You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 392Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 380Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 506Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 573Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 484Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 674Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 478The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 437Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 585Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 596Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 432All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 909Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 936Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 609Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 705Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 870Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 796You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 735For a undirected graph with tre ...
相关推荐
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the ...
144-Binary Tree Preorder Traversal94-Binary Tree Inorder Traversal145-Binary Tree Postorder Traversal(后续的非递归时间不够可以先跳过,有点难)层次遍历,队列辅助,相当于广搜。102-Binary Tree Level ...
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
python python_leetcode题解之094_Binary_Tree_Inorder_Traversal
javascript js_leetcode题解之94-binary-tree-inorder-traversal.js
c语言基础 c语言_leetcode题解之0094_binary_tree_inorder_traversal.zip
Javascript 的解决方案Leetcode Problems and interview problems in ...Inorder Traversal.js106 Construct Binary Tree from Inorder and Postorder Traversal.js107 Binary Tree Level Order Traversal II.js108 ...
`BinaryTree_InOrder.java`应该实现了这个过程,它使用递归或栈来实现。递归版本通常从左子节点开始,然后访问根节点,最后遍历右子节点。 3. **后序遍历(Postorder Traversal)** 后序遍历遵循“左-右-根”的...
在这个名为"Binary tree traversal.zip"的压缩包中,包含了作者在算法课上完成的一个关于二叉树遍历的作业。这个作业使用C++编程语言,并在Visual Studio 2019环境下编写。以下是关于二叉树遍历的详细知识: 1. **...
leetcode 530 力扣在线评委 # 问题 困难 解决方案 1 简单的 2 中等的 3 中等的 12 中等的 22 中等的 ...Binary Tree ...Traversal ...Binary Tree Inorder Traversal 318. Maximum Product of Word Length
Inorder Traversal 栈 递归 Single Number 异或 Copy List with Random Pointer 单链表 map Max Points on a Line 斜率 map, int> Fraction to Recurring Decimal map long long 正负号 Repeated DNA S
5. **二叉树与图**:二叉树题目如"Binary Tree Inorder Traversal"(二叉树的中序遍历)和"Lowest Common Ancestor of a Binary Tree"(二叉树的最近公共祖先),图题目如"Shortest Path in Bidirectional Graph"...
5. **递归与迭代**:如"Binary Tree Inorder Traversal"要求实现二叉树的中序遍历,递归和迭代都是常见的解题策略。 6. **设计模式**:一些题目会涉及单例模式、工厂模式等设计模式的应用,例如"Singleton"题目要求...
- 中序遍历 (Inorder Traversal) - 后序遍历 (Postorder Traversal) - 层次遍历 (Level Order Traversal) ##### 2.2 进阶操作 - **查找**: 在二叉树中查找特定的节点。 - **插入**: 向二叉树中插入新的节点。 -...
in_order_traversal(node.right) ``` 3. **后序遍历**(左-右-根):先遍历左子树,然后遍历右子树,最后访问根节点。 ```python def post_order_traversal(node): if node: post_order_traversal(node.left) ...