leetcode--Learn one iterative inorder traversal, apply it to multiple tree questions (Java Solution)
will show you all how to tackle various tree questions using iterative inorder traversal. First one is the standard iterative inorder traversal using stack. Hope everyone agrees with this solution.
Question : Binary Tree Inorder Traversal
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
while(root != null || !stack.empty()){
while(root != null){
stack.push(root);
root = root.left;
}
root = stack.pop();
list.add(root.val);
root = root.right;
}
return list;
}
Now, we can use this structure to find the Kth smallest element in BST.
Question : Kth Smallest Element in a BST
public int kthSmallest(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack<>();
while(root != null || !stack.isEmpty()) {
while(root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
if(--k == 0) break;
root = root.right;
}
return root.val;
}
We can also use this structure to solve BST validation question.
Question : Validate Binary Search Tree
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
Stack<TreeNode> stack = new Stack<>();
TreeNode pre = null;
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
if(pre != null && root.val <= pre.val) return false;
pre = root;
root = root.right;
}
return true;
}
leetcode--Learn one iterative inorder traversal, apply it to multiple tree questions (Java Solution)的更多相关文章
- Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space
题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- [LeetCode] 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 ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- [LeetCode]题解(python):094 Binary Tree Inorder Traversal
题目来源 https://leetcode.com/problems/binary-tree-inorder-traversal/ iven a binary tree, return the ino ...
- Binary Search Tree In-Order Traversal Iterative Solution
Given a binary search tree, print the elements in-order iteratively without using recursion. Note:Be ...
随机推荐
- pycharm安装 package报错:module 'pip' has no attribute 'main'
转自: <pycharm安装 package报错:module 'pip' has no attribute 'main'> https://www.cnblogs.com/Fordest ...
- 安装mariadb并修改配置文件
实验环境:CentOS7 #安装mariadb-server包#修改mariadb配置文件/etc/my.cnf.d/server.cnf#添加 skip_name_resolve=ON #不执行将I ...
- B/S测试与C/S测试之区别
我们在日常功能测试工作中,常常依据测试对象和测试目标的不同分为四个级别的测试,单元测试.集成测试.系统测试和验收测试,但是往往忽略了被测应用系统架构.在测试过程中针对不同的系统架构,测试的侧重点也不同 ...
- phpstorm断点调试 php.ini 文件中 Xdebug 配置
[XDebug]xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug"xdebug.trace_output_dir="D ...
- javascript面向对象的测试实例
- mysql主从服务器复制操作
master主机ip:192.168.1.19 用户名sunzy 密码123456slave主机ip:192.168.1.20 1.配置master1)接下来对master进行配置,包括打开二进制日 ...
- Python实现R包brainwaver中的compute.FDR函数
FDR(false discovery rate),是统计学中常见的一个名词,翻译为伪发现率,其意义为是 错误拒绝(拒绝真的(原)假设)的个数占所有被拒绝的原假设个数的比例的期望值. compute. ...
- p2921 Trick or Treat on the Farm
传送门 题目 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N个牛棚里转 悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果.农场不大,所以约翰要想尽法子让奶牛们得到快乐 ...
- 功防技术与实践第1.2章,kali初步了解
20169314 2016-2017-2 <网络攻防实践>/<网络攻击与防范>第2周学习总结 一.教材学习内容总结 1.hacker和cracker的区别 网络攻防分三部分:系 ...
- Dapper.Common基于Dapper的开源LINQ超轻量扩展
Dapper.Common Dapper.Common是基于Dapper的LINQ实现,支持.net core,遵循Linq语法规则.链式调用.配置简单.上手快,支持Mysql,Sqlserver(目 ...