Kth Smallest Element in a BST
来源:互联网 发布:mac开启root权限 编辑:程序博客网 时间:2024/06/07 13:22
工作时做一道题好难。。。
最基本的中序遍历!
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int kthSmallest(TreeNode root, int k) { if (root == null) { return 0; } Stack<TreeNode> stack = new Stack<>(); stack.push(root); int count = 1; while (!stack.isEmpty()) { TreeNode node = stack.peek(); if (node.left != null) { stack.push(node.left); node.left = null; } else { if (count == k) { return node.val; } count++; stack.pop(); if (node.right != null) { stack.push(node.right); } } } return 0; }}
0 0
- #leetcode#Kth Smallest Element in a BST
- 230.Kth Smallest Element in a BST
- leetcode--Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- Leetcode|Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- [leetcode] Kth Smallest Element in a BST
- 【LeetCode】Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- 【leetcode】Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- leetcode: Kth Smallest Element in a BST
- NKOJ 2150 广告印刷 单调队列
- (java)Tomcat项目启动时内存溢出问题
- linux下的lsof的使用及其在取证分析时的应用
- Mac配置Groovy开发环境
- 在Eclipse中运行JAVA代码远程操作HBase的示例
- Kth Smallest Element in a BST
- Find the Thief (Facebook Interview)
- 《java编程思想》学习笔记(二)
- USB OTG原理+ ID 检测原理
- python常用技巧
- pcap文件解析
- 应用RecyclerView实现Gallery相册效果——注意引入recyclerview-v7的版本
- Android之Activity(一):活动简介
- Linux--内核Uevent事件机制 与 Input子系统