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 ...
 
随机推荐
- easy_install下载地址及安装
			
下载地址 https://pypi.python.org/pypi/setuptools 解压 tar -xzvf xx.tar.gz 安装 cd 解压目录 sudo python setup.py ...
 - 添加一个用户并且让用户获得root权限
			
1.创建一般用户: 完全参考默认值创建一个用户, 一般账号UID应该是500以后的. 默认会创建用户家目录和账号一模一样的群组名.创建使用账号且给予口令才算完成了用户的创建流程. useradd us ...
 - HDU1698(线段树入门题)
			
Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
 - unittest 执行测试脚本输出测试报告
			
import unittestimport HTMLTestRunnertest as HTMLTestRunner#获取路径path = './'#创建测试套件,读取测试脚本suite = unit ...
 - kvm基础  虚拟机内存、CPU调整
			
转自http://blog.csdn.net/hnhuangyiyang/article/details/50902223 一.调小虚拟机内存 调小虚拟机内存可以动态实现,不用关机1.查看当前内存大小 ...
 - BluetoothFindFirstRadio 函数
			
HBLUETOOTH_RADIO_FIND BluetoothFindFirstRadio( BLUETOOTH_FIND_RADIO_PARAMS* pbtfrp, HANDLE* phRadio ...
 - 0010_while循环
			
while 条件: 代码块 break:跳出循环语句 continue:跳出本次循环,进入下一次循环. __author__ = 'qq593' #!/usr/bin/env python #-*- ...
 - [51nod1247]可能的路径(思维题)
			
题意:给定(a,b),(x,y) ,(a,b)可以通向(a-b,b) (a+b,b) (a,a+b) (a,a-b) 求能否到达(x,y) 解题关键:类似于更相减损,变换过程中gcd是一样的. #i ...
 - windows、Linux  开放端口
			
一.Linux开放端口: 1. CentOS7.x/RedHat7.x , 参考 CentOS7使用firewalld打开关闭防火墙与端口 1.firewalld的基本使用 启动: systemct ...
 - uboot——详解各目录下的文件作用
			
uboot下载地址:http://ftp.denx.de/pub/u-boot/ 1.目录分布 2.目录结构变化: u-boot-2010.03及以前版本 ├── api ...