题目链接:点击打开链接
题目大意:略
解题思路:略
相关企业
- 字节跳动
AC 代码
- Java
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/classSolution { intth; List<Integer>list=newArrayList<>(); publicintkthLargest(TreeNoderoot, intk) { th=k; dfs(root); returnlist.get(k-1); } voiddfs(TreeNodenode) { if (node==null||list.size() ==th) { return; } dfs(node.right); list.add(node.val); dfs(node.left); } }
- C++
classSolution { public: intkthLargest(TreeNode*root, intk) { this->k=k; dfs(root); returnres; } private: intres, k; voiddfs(TreeNode*root) { if(root==nullptr) return; dfs(root->right); if(k==0) return; if(--k==0) res=root->val; dfs(root->left); } };