【第一题】二叉树的层平均值
分析:涉及到一层一层,应想到层序遍历(Queue)
//执行用时:2 ms, 在所有 Java 提交中击败了98.96% 的用户
//内存消耗:40.3 MB, 在所有 Java 提交中击败了70.87% 的用户
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
List<Double> list = new ArrayList<>();
if(root == null){return list;}
q.add(root);
while(q.size()!=0){
double sum = 0;
int qSize = q.size();
for(int i = qSize;i>0;i--){
TreeNode node = q.poll();
sum += node.val;
if(node.left !=null){
q.add(node.left);
}
if(node.right != null){
q.add(node.right);
}
}
list.add(sum / qSize);
}
return list;
}
}
【第二题】二叉搜索树节点的最小距离
分析:二叉搜索树的中序遍历就是一个有序数组。首先可以遍历一遍搜索树,然后将值存到数组里
//执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:36.1 MB, 在所有 Java 提交中击败了34.73% 的用户
class Solution {
private TreeNode pre = null;
private int minGap = Integer.MAX_VALUE;
public int minDiffInBST(TreeNode root) {
dfs(root);
return minGap;
}
private void dfs(TreeNode root){
if(root == null){return;}
dfs(root.left);
//单层逻辑
if(pre!=null){
minGap = Math.min(root.val-pre.val,minGap);
}
pre = root;
dfs(root.right);
}
}
【第三题】二叉搜索树中的搜索
分析:
//执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:39.2 MB, 在所有 Java 提交中击败了79.42% 的用户
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root == null || root.val == val){return root;}
return root.val > val?searchBST(root.left,val):searchBST(root.right,val);
}
}
【第四题】二叉搜索树的范围和
分析:
在这里插入代码片
【第五题】N叉树的后序遍历
分析:首先准备一个栈用来存放父节点,然后准备一个容器,每次取栈中的顶部节点,然后存起来,每弹出一个节点,就压入他的子节点
最后要完成一个倒序,因此该容器可以是list,然后Collection(list.reverse);或者是第二个栈,然后依次弹出。
//执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:38.9 MB, 在所有 Java 提交中击败了93.58% 的用户
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> postorder(Node root) {
//Stack<TreeNode> s = new LinkedList<>();
if(root == null){return list;}
for(int i = 0;i < root.children.size();i++){
postorder(root.children.get(i));
}
list.add(root.val);
return list;
}
}
class Solution {
public List<Integer> postorder(Node root) {
List<Integer> list = new ArrayList<>();
if(root == null){return list;}
Stack<Node> s1 = new Stack<>();
Stack<Node> s2 = new Stack<>();
s1.push(root);
while(!s1.isEmpty()){
Node node = s1.pop();
//每次将s1顶部节点(每个父节点)压入s2
s2.push(node);
//然后将弹出节点的子节点压入s1
for(int i = 0;i < node.children.size();i++){
s1.push(node.children.get(i));
}
}
while(!s2.isEmpty()){
list.add(s2.pop().val);
}
return list;
}
}
【第六题】二叉树的镜像
分析:注意不是兄弟节点值交换啊!!是对称位置上的节点值交换。
方法一:将左右节点交换,要注意没有节点的情况(也就是左右孩子存在其中一个缺失),这时要新建节点。
假如交换值,那么还得考虑null的情况,但是直接交换节点(或者说节点的地址),那么就相当于把节点以及其子树也交换过来了。
//递归
//执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:35.9 MB, 在所有 Java 提交中击败了37.30% 的用户
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root == null){return null;}
if(root.left == null && root.right == null){return root;}
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
mirrorTree(root.left);
mirrorTree(root.right);
return root;
}
}
class Solution {
public TreeNode mirrorTree(TreeNode root) {
Stack<TreeNode> s = new LinkedList<>();
if(root == null){return root;}
if(root.left == null && root.right == null){return root;}
s.push(root);
while(!s.isEmpty()){
TreeNode node = s.pop();
if(node !=null){continue;}
swap(node.left,node.right);
if(node.right != null){s.push(node.right);}
if(node.left !=null){s.push(node.left);}
}
return root;
}
}
【第七题】BiNode
分析:二叉搜索树的中序遍历就是有序数组,然后顺着中序遍历的方向构建二叉树,将左子树统统设置为null;
//执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:43.8 MB, 在所有 Java 提交中击败了90.64% 的用户
class Solution {
//新建了一个先驱节点
TreeNode f = null;
public TreeNode convertBiNode(TreeNode root) {
f = new TreeNode(Integer.MIN_VALUE);
TreeNode pre = f;
dfs(root);
return pre.right;
}
public void dfs(TreeNode root){
if(root == null){return;}
dfs(root.left);
f.right = root;
//把之前的树断了,但是f连接的树还在
root.left = null;
//连接所有root
f = f.right;
dfs(root.right);
}
}
【第八题】二叉树的最近共同祖先
分析:
方法一:递归。
假如是根节点的不同子树,那么公共祖先即根节点;
假如是根节点同一子树
方法二:前序遍历根节点到p和到q的两个路径(注意剪枝),然后两个路径逐个比对,最后一个相同的节点就是公共节点。
这个方法可以用map实现(这种方式也可以用于单向链表的反向遍历),也可以用list实现
//执行用时:7 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:39.7 MB, 在所有 Java 提交中击败了73.50% 的用户
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null){return null;}
if(root == p || root == q){
return root;
}
//一个在左子树查找
TreeNode Left = lowestCommonAncestor(root.left,p,q);
//一个在右子树查找
TreeNode right = lowestCommonAncestor(root.right,p,q);
//在不同子树,返回根节点
if(left != null && right !=null){
return root;
}
//都在左子树
if(left != null){
return left;
}
//都在右子树
if(right !=null){
return right;
}
//至少有一个没找到
return null;
}
}
_______________________
//简化版
class Solution{
public TreeNode lowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q){
if(root == null || p == root || q == root){
return root;
}
TreeNode l = lowestCommonAncestor(root.left,p,q);
TreeNode r = lowestCommonAncestor(root.right,p,q);
//等价于说,l == null,返回r;l!=null,r=null,返回l;两者都不为null,返回root
return l == null?r:(r == null ? l:root);
}
}
class Solution{
//建立一个哈希表,存储结点及其父节点
Map<Integer,TreeNode> parent = new HashMap<Integer,TreeNode>;
//记录已经访问过的结点
Set<Integer> visited = new HashSet<Integer>();
public void dfs(TreeNode root){
if(root.left != null){
map.put(root.left.val,root);
dfs(root.left);
}
if(root.right != null){
map.put(root.right.val,root);
dfs(root.right);
}
}
public TreeNode lowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q){
dfs(root);
while(p!=null){
visited.add(p.val);
p = parent.get(p.val);
}
while(q != null){
//假如说p的路径上包含q,那么说明q是p的父节点之一
if(visited.contains(q.val)){
return q;
}
//不断迭代找q的父节点
q = parent.get(q.val);
}
return null;
}
}
【第九题】将有序数组转化为二叉搜索树
分析:
//二叉树中序遍历的逆过程
//执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:38.4 MB, 在所有 Java 提交中击败了28.95% 的用户
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return nums == null ? null:buildTree(nums,0,nums.length - 1);
}
private TreeNode buildTree(int[] nums,int l,int r){
if(r < l){return null;}
int m = l + (r-l)/2; //找到两者中点
TreeNode root = new TreeNode(nums[m]);
root.left = buildTree(nums,l,m-1);
root.right = buildTree(nums,m+1,r);
return root;
}
}
【第十题】另一个树的子树
分析:首先要确定t是不是s的一个子树;另外要确定t的节点值是不是包含在s中。
方法一:在s中找到t的根节点,然后将其地址给新节点(也就是摘除该子树),然后和t一一比对,比对结构和值。
方法二:t是s的子树有三种情况——t=s(两棵树相同);t是s的左子树;t是s的右子树
//执行用时:4 ms, 在所有 Java 提交中击败了99.09% 的用户
//内存消耗:38.2 MB, 在所有 Java 提交中击败了98.59% 的用户
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if(s == null && t == null){return true;}
if(s == null && t != null){return false;}
return isSubtree(s.left,t) || isSubtree(s.right,t) || isSameTree(s,t);
}
private boolean isSameTree(TreeNode s,TreeNode t){
if(s == null && t == null){
return true;
}
if(s == null || t == null){return false;}
return s.val == t.val
&& isSameTree(s.left,t.left)
&& isSameTree(s.right,t.right);
}
}
【第十一题】检查平衡性
分析:对于每个结点,递归查找左子树与右子树的高度,然后判断深度差是否超过1。
//执行用时:1 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:38.3 MB, 在所有 Java 提交中击败了87.95% 的用户
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null){return true;}
//if(root.left == null && root.right == null){return true;}
return (Math.abs(getHigh(root.left)- getHigh(root.right)) < 2 && isBalanced(root.left) && isBalanced(root.right));
}
private int getHigh(TreeNode root){
return root == null ?0:Math.max(getHigh(root.left),getHigh(root.right)) + 1;
}
}
【第十二题】二叉搜索树中的众数
分析:中序遍历,然后设置一个变量pre
class Solution {
private int pre = 0,count = 0,maxCount = 0;
LinkedList<Integer> list = new LinkedList<>();
public int[] findMode(TreeNode root) {
dfs(root);
int[] ans = new int[list.size()];
//将列表每个数都存储在数组中
for(int i = 0;i < ans.length;i++){
ans[i] = list.removeFirst();
}
return ans;
}
//pre代表前一个众数,count为当前数的计数,maxCount指最大数目
public void dfs(TreeNode root){
if(root == null){return;}
dfs(root.left);
//count更新
if(root.val == pre){
count++;
}else{
pre=root.val;
count = 1;
}
//结果集合更新(发现大于原来的,马上clear清空)
if(count == maxCount){
list.add(root.val);
}else if(count > maxCount){
list.clear();
list.add(root.val);
maxCount = count;
}
dfs(root.right);
}
}
【第十三题】特定深度结点链表
分析:其实这就是层序遍历。但是这个链表数组是啥呀
class Solution {
public ListNode[] listOfDepth(TreeNode tree) {
if(tree == null){return new ListNode[0];}
Queue<TreeNode> q = new LinkedList<>();
q.add(tree);
//新建一个列表
List<ListNode> list = new ArrayList<>();
//新建一个链表
ListNode node = new ListNode(-1);
while(!q.isEmpty()){
//设置一个头结点,标识单向链表
ListNode pre = node;
int count = q.size();
for(int i = 0;i < count;i++){
tree = q.poll();
pre.next = new ListNode(tree.val);
pre = pre.next;
if(tree.left !=null){
q.add(tree.left);
}
if(tree.right != null){
q.add(tree.right);
}
}
list.add(node.next);
}
//将列表中的每个链表加入到链表数组
ListNode[] ret = new ListNode[list.size()];
for(int i = 0;i < list.size();i++){
ret[i] = list.get(i);
}
return ret;
}
}
【第十四题】不同的二叉搜索树II
分析:将有序数组转化为二叉搜索树,生成所有不同结构的二叉搜索树。(==二分法的边界控制)
第一种:总是选择中点右边的节点作为根节点;(left+right+1)/2
第二种:总是选择中点左边的节点作为根节点;(left+right)/2
第三种:随机选择中点两边的节点作为根节点。
分别递归左子树和右子树,然后从可行左子树和可行右子树中选一颗拼接在根节点上,并将生成的二叉搜索树加入答案数组即可
class Solution{
public List<TreeNode> generateTrees(int n) {
//首先判空
//然后调用递归函数
}
public List<TreeNode> generateTrees(int left,int right){
//继续判空,递归的出口
//枚举可行的根节点
//递归左子树
//递归右子树
//从左子树集合中选出一棵,从右子树集合中选出一棵,拼接到根节点上
}
}
【第十五题】不同的二叉搜索树
分析:假设n个节点存在二叉排序树的个数是G(n),那么从1-n分别作为根节点,那么两边左右子树的数目之和为(n-1),组合起来的树的种数为G(0)*G(n-1) + G(1) *G(n-2)…+G(n-1)*G(0)
//执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:35 MB, 在所有 Java 提交中击败了84.06% 的用户
class Solution {
//建立哈希表保存计算过的节点,避免重复计算
Map<Integer,Integer> map = new HashMap<>();
public int numTrees(int n) {
return helper(1,n);
}
public int helper(int left,int right){
if(map.containsKey(right - left)){
return map.get(right - left);
}
if(left > right){
return 1;
}
int sum = 0;
for(int i = left;i <= right;i++){
//以i为根节点时左右子树的个数
int leftSize = helper(left,i-1);
int rightSize = helper(i+1,right);
sum+= leftSize * rightSize;
}
map.put(right - left,sum);
return sum;
}
}
【第十六题】二叉树展开为列表
分析:注意,这里是直接修改原树,不能新建一个链表。
class Solution {
TreeNode last= null;
public void flatten(TreeNode root) {
if(root == null)return;
//直接从最右边结点开始
flatten(root.right);
flatten(root.left);
root.right = last;
root.left = null;
last = root;
}
}
【第十七题】从前序与中序遍历序列构造二叉树
分析:
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length == 0||inorder.length == 0){
return null;
}
TreeNode root = new TreeNode(preorder[0]);
for(int i = 0;i < preorder.length;i++){
if(inorder[i] == preorder[0]){
root.left = buildTree(Arrays.copyOfRange(preorder,1,i+1),Arrays.copyOfRange(inorder,0,i));
root.right = buildTree(Arrays.copyOfRange(preorder,i+1,preorder.length),Arrays.copyOfRange(inorder,i+1,inorder.length));
break;
}
}
return root;
}
}
【第十八题】找出克隆二叉树中的相同结点
分析:直接在原树和克隆树中同时搜索,因为两棵树的结构一样,当值相同的时候,可以通过比较是不是两棵树的同一位置来判断。
//执行用时:1 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:46 MB, 在所有 Java 提交中击败了57.22% 的用户
class Solution {
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
if(original ==null){return null;}
if(original == target){return cloned;}
TreeNode left = getTargetCopy(original.left,cloned.left,target);
if(left!=null) return left;
return getTargetCopy(original.right,cloned.right,target);
}
}
【第十九题】层数最深叶子结点的和
分析:首先要知道最深层数,其次要累加该层的结点和。()
class Solution {
private int sum = 0;
public int deepestLeavesSum(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
if(root == null){return 0;}
q.add(root);
while(!q.isEmpty()){
int count = q.size();
sum = 0;
for(int i = 0;i < count;i++){
TreeNode node = q.poll();
sum += node.val;
if(node.left!=null){
q.add(node.left);
}
if(node.right!=null){
q.add(node.right);
}
}
}
return sum;
}
}
【第二十题】最大二叉树
分析:每次挑出最大值作为根节点,然后左孩子的左节点为空,右孩子的右节点为空。
有个问题,根节点是有左右孩子的,但是其他的子树(左子树的结点没有左孩子,右子树的结点没有右孩子)。
//执行用时:2 ms, 在所有 Java 提交中击败了99.98% 的用户
//内存消耗:38.7 MB, 在所有 Java 提交中击败了47.73% 的用户
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return maxTree(nums, 0, nums.length - 1);
}
public TreeNode maxTree(int[] nums, int l, int r){
if(l > r){
return null;
}
//bond为当前数组中最大值的索引
int bond = findMax(nums, l, r);
TreeNode root = new TreeNode(nums[bond]);
root.left = maxTree(nums, l, bond - 1);
root.right = maxTree(nums, bond + 1, r);
return root;
}
//找最大值的索引
public int findMax(int[] nums, int l, int r){
int max = Integer.MIN_VALUE, maxIndex = l;
for(int i = l; i <= r; i++){
if(max < nums[i]){
max = nums[i];
maxIndex = i;
}
}
return maxIndex;
}
}
【第二十一题】祖父节点值为偶数的节点和
分析:
【第二十二题】好叶子节点对的数量
分析:用哈希表记录到每个节点以及其父节点的关系。然后给其中一个节点的路径建立一个集合,在另一个叶子节点回溯时,也在set中查找有没有重合的点(同时也记录距离),然后找到共同的节点之后,将两个节点到该公共节点的距离相加即可。
但是问题来了,如何判断哪些是叶子节点?
方法二:每走到一个节点,记录left和right的深度,然后保存distance。遍历到X结点时,针对其左右子树,分别求它们所有的叶子深度并存到lefts和rights里,再计算左右子树的叶子和X结点链接起来的长度(即左子树叶子深度集合和右子树叶子深度集合中的元素互相之和再加2)是否<=distance,是的话res++。继续dfs遍历重复以上操作。
class Solution {
public int countPairs(TreeNode root, int distance) {
if(root == null){return 0;}
List<Integer> lefts = new LinkedList<>();
countDepth(root.left,0,lefts);
List<Integer> rights = new LinkedList<>();
countDepth(root.right,0,rights);
int res = 0;
for(Integer l:lefts)
for(Integer r:rights)
if(l+r+2 <= distance)
res++;
res += countPairs(root.left,distance);
res += countPairs(root.right,distance);
return res;
}
void countDepth(TreeNode node,int depth,List<Integer> list){
if(node== null) return;
if(node.left == null && node.right == null){
list.add(depth);
return;
}
countDepth(node.left,depth+1,list);
countDepth(node.right,depth+1,list);
}
}
class Solution {
public int countPairs(TreeNode root, int distance) {
Pair pair = dfs(root, distance);
return pair.count;
}
// 对于 dfs(root,distance),同时返回:
// 1)每个叶子节点与 root 之间的距离
// 2) 以 root 为根节点的子树中好叶子节点对的数量
public Pair dfs(TreeNode root, int distance) {
int[] depths = new int[distance + 1];
boolean isLeaf = root.left == null && root.right == null;
if (isLeaf) {
depths[0] = 1;
return new Pair(depths, 0);
}
int[] leftDepths = new int[distance + 1];
int[] rightDepths = new int[distance + 1];
int leftCount = 0, rightCount = 0;
if (root.left != null) {
Pair leftPair = dfs(root.left, distance);
leftDepths = leftPair.depths;
leftCount = leftPair.count;
}
if (root.right != null) {
Pair rightPair = dfs(root.right, distance);
rightDepths = rightPair.depths;
rightCount = rightPair.count;
}
for (int i = 0; i < distance; i++) {
depths[i + 1] += leftDepths[i];
depths[i + 1] += rightDepths[i];
}
int cnt = 0;
for (int i = 0; i <= distance; i++) {
for (int j = 0; j + i + 2 <= distance; j++) {
cnt += leftDepths[i] * rightDepths[j];
}
}
return new Pair(depths, cnt + leftCount + rightCount);
}
}
class Pair {
int[] depths;
int count;
public Pair(int[] depths, int count) {
this.depths = depths;
this.count = count;
}
}
【第二十三题】二叉树剪枝
分析:判断以node为根的子树中是否包含1的条件是:当且仅当node的左右孩子为根的子树均不包含1,并且其本身的值也不为1.
class Solution{
public TreeNode pruneTree(TreeNode root){
return containsOne(root)?root:null;
}
public boolean containsOne(TreeNode node){
if(node == null) return false;
boolean a1 = containsOne(node.left);
boolean a2 = containsOne(node.right);
if(!a1) node.left = null;
if(!a2) node.right = null;
return node.val == 1||a1||a2;
}
}
【第二十四题】两颗二叉搜索树中的所有元素
分析:二叉搜索树——>有序列表,直接推 中序遍历。
中序遍历+列表排序;
中序遍历+归并排序;
//执行用时:20 ms, 在所有 Java 提交中击败了81.36% 的用户
//内存消耗:41.6 MB, 在所有 Java 提交中击败了19.83% 的用户
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
if(root1 == null && root2 == null){return list;}
InOrder(root1,list);
InOrder(root2,list);
Collections.sort(list);
return list;
}
public List<Integer> InOrder(TreeNode root,List<Integer> list){
if(root == null){
return list;
}
InOrder(root.left,list);
list.add(root.val);
InOrder(root.right,list);
return list;
}
}
class Solution {
public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
getMidOrder(root1, list1);
getMidOrder(root2, list2);
List<Integer> res = new ArrayList<>();
int i = 0, j = 0;
while(i < list1.size() || j < list2.size()) {
if(j >= list2.size()) {
res.add(list1.get(i++));
}else if(i >= list1.size()) {
res.add(list2.get(j++));
}else if(list1.get(i) <= list2.get(j)) {
res.add(list1.get(i++));
}else {
res.add(list2.get(j++));
}
}
return res;
}
private void getMidOrder(TreeNode root, List<Integer> list) {
if(root == null) {
return;
}
getMidOrder(root.left, list);
list.add(root.val);
getMidOrder(root.right, list);
}
}
【第二十五题】删除给定值的叶子节点
分析:
1、搞一个后续遍历,左孩子、右孩子、父节点的删除顺序。
class Solution {
public TreeNode removeLeafNodes(TreeNode root, int target) {
if(root == null) return null;
root.left = removeLeafNodes(root.left,target);
root.right = removeLeafNodes(root.right,target);
if(root.val == target && root.left == null && root.right == null){return null;}
return root;
}
}
【第二十六题】二叉搜索树中第K小的元素
分析:中序遍历即可.
方法一:迭代;
方法二:递归。(递归的话,count应该放在哪)
//执行用时:1 ms, 在所有 Java 提交中击败了45.78% 的用户
//内存消耗:38 MB, 在所有 Java 提交中击败了94.98% 的用户
//迭代版
class Solution {
public int kthSmallest(TreeNode root, int k) {
if(root == null){return 0;}
Stack<TreeNode> s = new Stack<>();
List<Integer> list = new ArrayList<>();
s.push(root);
while(!s.isEmpty()){
while(root!=null){
s.push(root);
root = root.left;
}
TreeNode node = s.pop();
list.add(node.val);
if(list.size() == k){
break;
}
root = node.right;
}
return list.get(list.size()-1);
}
}
//执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
//内存消耗:38.2 MB, 在所有 Java 提交中击败了74.84% 的用户
//递归版
class Solution {
int count = 0;
int res = 0;
public int kthSmallest(TreeNode root, int k) {
inOrder(root,k);
return res;
}
public void inOrder(TreeNode root,int k){
if(root == null){return;}
inOrder(root.left,k);
count++;
if(count == k){
res = root.val;
return;
}
inOrder(root.right,k);
}
}