Binary Tree Inorder Traversal LeetCode Java
来源:互联网 发布:数据库管理系统例子 编辑:程序博客网 时间:2024/05/19 00:55
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]
.
Note: Recursive solution is trivial, could you do it iteratively?
Check code in detail below:
Key to solve: Stack
In_Order: left -> mid -> right
Push all the left subtree node into Stack, when we reach bottom of left subtree
As a result, the top element of Stack will be left most bottom node of Tree,
It is time to pop elemnt from stack and add into result list,
After left subTree -> Continue traverse right subTree, until stack is empty
public static ArrayList<Integer> inorderTraversal(TreeNode root) { ArrayList<Integer> res=new ArrayList<Integer>(); if(root==null) return res; Stack<TreeNode> stack=new Stack<TreeNode>(); while(!stack.isEmpty() || root!=null){ //push all the left node of tree into stack first if(root!=null){ stack.push(root); root=root.left; }else{ //reach the bottom of left subtree, pop out from stack root=stack.pop(); //add into result list res.add(root.val); root=root.right; } } return res; }
0 0
- [Leetcode] Binary Tree Inorder Traversal (Java)
- Binary Tree Inorder Traversal LeetCode Java
- 【Leetcode】Binary Tree Inorder Traversal in JAVA
- [LeetCode][Java] Binary Tree Inorder Traversal
- leetcode-94:Binary Tree Inorder Traversal (Java)
- leetcode:Binary Tree Inorder Traversal 【Java】
- LeetCode 94 -Binary Tree Inorder Traversal ( JAVA )
- leetcode-java-94. Binary Tree Inorder Traversal
- 【leetcode】94. Binary Tree Inorder Traversal【java】
- LeetCode: Binary Tree Inorder Traversal
- [LeetCode]Binary Tree Inorder Traversal
- LeetCode Binary Tree Inorder Traversal
- [Leetcode] Binary Tree Inorder Traversal
- Leetcode: Binary Tree Inorder Traversal
- LeetCode Binary Tree Inorder Traversal
- LeetCode : Binary Tree Inorder Traversal
- [Leetcode] Binary Tree Inorder Traversal
- 【leetcode】Binary Tree Inorder Traversal
- c++爬虫引擎
- 7月十号笔记
- Windows--某些设置由你的系统管理员管理
- 免安装版mysql配置
- Spark1.0.0 开发环境快速搭建
- Binary Tree Inorder Traversal LeetCode Java
- js中的字符串(string)的一些方法:toUpperCase(),indexOf(""),length
- 编程算法 - 和为s的连续正整数序列 代码(C)
- linux下find,xargs命令详解和实例
- Linux下ctrl+c/z/d的区别
- 第26条:优先考虑泛型
- ios基础——OC语法代码块的运用
- 交叉编译ortp、osip2、eXosip2、mediastreamer2及其附带的库实现SIP软电话
- 为什么说知识改变命运?