Leetcode-94. Binary Tree Inorder Traversal
来源:互联网 发布:java api百度云盘 编辑:程序博客网 时间:2024/05/20 00:13
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3]
,
1 \ 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
这个题目讲道理如果用递归太简单了,如果不用递归的话,我会考虑维护一个堆,每次把root结点压到堆里面,然后弹出的时候首先输出自己的值然后把右节点压入堆中。时间缘故就不用堆的方法实现了。Your runtime beats 39.24% of java submissions.public class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> results = new ArrayList<Integer>();generateInorder(results,root);return results; } public void generateInorder(List<Integer> results,TreeNode root){if(root!=null){generateInorder(results,root.left);results.add(root.val);generateInorder(results,root.right);} }}
0 0
- LeetCode 94. Binary Tree Inorder Traversal
- [LeetCode]94.Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal Leetcode Python
- [Leetcode] 94. Binary Tree Inorder Traversal
- LeetCode --- 94. Binary Tree Inorder Traversal
- LeetCode 94. Binary Tree Inorder Traversal
- [leetcode] 94.Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal LeetCode
- LeetCode 94. Binary Tree Inorder Traversal
- *LeetCode 94. Binary Tree Inorder Traversal
- [LeetCode]94. Binary Tree Inorder Traversal
- [LeetCode]94. Binary Tree Inorder Traversal
- 【LeetCode】94. Binary Tree Inorder Traversal
- leetcode 94. Binary Tree Inorder Traversal
- leetcode 94. Binary Tree Inorder Traversal
- LeetCode *** 94. Binary Tree Inorder Traversal
- LeetCode-94.Binary Tree Inorder Traversal
- LeetCode 94. Binary Tree Inorder Traversal
- HDU 3336 Count the string 【KMP】【dp】
- swift switch选择结构
- javascript中的 事件的绑定1
- Android之二维码生成与识别
- 文章标题
- Leetcode-94. Binary Tree Inorder Traversal
- 跳石板(待改进)
- tcnative-1.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform
- 日常潜水-20161013-进一步细化ping IP地址结果导出成csv文件
- 嵌入式按键驱动,支持短按、长按、双击(中断方式)
- hdu5544 - Ba Gua Zhen 按位贪心+xor性质
- Unity生成.assets
- javascript中的 事件的绑定 2
- 【搞事情】 学习如何写一个网页上的交互游戏 【计划篇】