LeetCode Kth Smallest Element in a BST DFS
来源:互联网 发布:域名注册软件 编辑:程序博客网 时间:2024/05/19 18:00
思路:
方法一 & 二:
中序遍历BST,第K个遍历到的节点就是第K小的元素。
缺点:每次找都要遍历整个BST,需要O(N)的时间复杂度(其中N为节点个数)和维护一个栈的开销。题目给了hint:可以修改树的节点的结构使得复杂度降低到O(logN)。
java code(iterative):
/** * 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> s = new Stack<TreeNode>(); TreeNode node = root; while(node != null) { s.push(node); node = node.left; } int count = 0; while(!s.empty() && count < k) { node = s.peek(); s.pop(); count++; TreeNode cur = node.right; while(cur != null) { s.push(cur); cur = cur.left; } } return node.val; }}
java code:(DFS)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private int number = 0; private int count = 0; private void dfs(TreeNode cur) { if(cur.left != null) { dfs(cur.left); } count--; if(count == 0) { number = cur.val; return; } if(cur.right != null) { dfs(cur.right); } } public int kthSmallest(TreeNode root, int k) { count = k; dfs(root); return number; }}
方法三:
计算每个节点的左子树的节点个数:
如果左子树的节点个数 >= k :则从左子树中找;
如果左子树的节点个数 + 1== k :则当前节点就是要找的节点;
如果左子树的节点个数 + 1 < k:则从右子树中找第 k - count - 1 的节点就好;
时间复杂度:O(logN)。
java code:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private int countNodes(TreeNode node) { if(node == null) return 0; return 1 + countNodes(node.left) + countNodes(node.right); } public int kthSmallest(TreeNode root, int k) { int count = countNodes(root.left); if(k <= count) {//in the bst's left side return kthSmallest(root.left, k); }else if(k == count + 1) { return root.val; }else {//k > count + 1 return kthSmallest(root.right, k - count - 1); } }}
0 0
- LeetCode Kth Smallest Element in a BST DFS
- Leetcode 230 - Kth Smallest Element in a BST(dfs)
- #leetcode#Kth Smallest Element in a BST
- leetcode--Kth Smallest Element in a BST
- Leetcode|Kth Smallest Element in a BST
- [leetcode] Kth Smallest Element in a BST
- 【LeetCode】Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- 【leetcode】Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- leetcode: Kth Smallest Element in a BST
- Leetcode: Kth Smallest Element in a BST
- [LeetCode] Kth Smallest Element in a BST
- [LeetCode]Kth Smallest Element in a BST
- [Leetcode]Kth Smallest Element in a BST
- leetcode--Kth Smallest Element in a BST
- 【leetcode】Kth Smallest Element in a BST
- [leetcode] Kth Smallest Element in a BST
- cruskal算法求最小支撑树 打印支撑树的边集合 带图例
- latex: 在article文档环境(multicols)中, 使用begin{figure}无法显示的问题
- 计算两个中文字符串相似度——编辑距离算法
- gfirefly 学习之ubuntu 15 python 配置python (2)
- android 网络编程(二、IO流)
- LeetCode Kth Smallest Element in a BST DFS
- 名词
- IplImage, Mat, CvMat之间转换
- 之前项目中用到Spring+Quartz实现定时任务的配置方法
- IOS开发----UISearchBar,搜索框的使用
- 【转】asp调用C#编写的DLL发送邮件
- [转载]关于window下rNode.js的本地包路径过长,导致一些功能无法使用
- 使用AIDL实现进程间的通信
- 定位线程Segment fault (SIGSEGV)的方法