由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 大虾帮忙看看代码,为什么 res参数传入不了,返回总是null
相关主题
回馈本版,新鲜店面,新题新气象请问怎么样才能想到关于tree等比较简洁的答案呢?同时记一下超慢速刷题过半以鼓励自己
热腾腾的 LinkedIn 电面题攒RP请教一道g算法题
一道google面试题FB面经
Interview question::求教Leetcode题目:Lowest Common Ancestor
问题在哪儿啊 kth Node of BST,大家帮忙Find a sub tree with min weight怎么做
发现一个很恶心的基础问题渣渣cs本科半应届如何找工作
Find the node with given value in binary tree in in-order算法题:如何判断二叉树是AVL?
一道在线题请教个G题目
相关话题的讨论汇总
话题: treenode话题: root话题: null话题: node
进入JobHunting版参与讨论
1 (共1页)
h*********o
发帖数: 230
1
直接print是可以的,如果我想返回节点的话,总是空的,为什么 啊?
大虾们 帮忙解释一下啊
public static TreeNode deepestTreeNode(TreeNode root){
if(root==null)
return null;
int height=treeHeight(root);
TreeNode res=null;
deepestTreeNode(root,height-1,0, res);
return res;

}

public static void deepestTreeNode(TreeNode root, int h,int start,
TreeNode node){
if(root==null)
return;
if(start==h){
node=root;
// System.out.println(root.val);
return;
}
else{
deepestTreeNode(root.left,h,start+1,node);
deepestTreeNode(root.right,h,start+1,node);
}

}
d**e
发帖数: 6098
2
因为一开始res=null,没有任何地址可言
你可以将它放到另一个class里
class TreeNodeWithHeight {
private int height;
private TreeNode node;
public TreeNodeWithHeight() {
height = 0;
node = null;
}
public TreeNodeHeight(int height, TreeNode node) {
this.height = height;
this.node;
}
public void setHeight(int height) {...}
public void setNode(TreeNode node) {...}
public int getHeight() {...}
public int getNode() {...}
}
-----------
TreeNodeWithHeight res = TreeNodeWithHeight();
然后在里面用res.setNode
出来后再return res.getNode()

【在 h*********o 的大作中提到】
: 直接print是可以的,如果我想返回节点的话,总是空的,为什么 啊?
: 大虾们 帮忙解释一下啊
: public static TreeNode deepestTreeNode(TreeNode root){
: if(root==null)
: return null;
: int height=treeHeight(root);
: TreeNode res=null;
: deepestTreeNode(root,height-1,0, res);
: return res;
:

h*********o
发帖数: 230
3
刚才想通了,多谢。
因为是 reference type

【在 d**e 的大作中提到】
: 因为一开始res=null,没有任何地址可言
: 你可以将它放到另一个class里
: class TreeNodeWithHeight {
: private int height;
: private TreeNode node;
: public TreeNodeWithHeight() {
: height = 0;
: node = null;
: }
: public TreeNodeHeight(int height, TreeNode node) {

1 (共1页)
进入JobHunting版参与讨论
相关主题
请教个G题目问题在哪儿啊 kth Node of BST,大家帮忙
Lowest Common Ancestor发现一个很恶心的基础问题
Amazon 打印给定node距离最近的K个nodesFind the node with given value in binary tree in in-order
Twitter电面未通过一道在线题
回馈本版,新鲜店面,新题新气象请问怎么样才能想到关于tree等比较简洁的答案呢?同时记一下超慢速刷题过半以鼓励自己
热腾腾的 LinkedIn 电面题攒RP请教一道g算法题
一道google面试题FB面经
Interview question::求教Leetcode题目:Lowest Common Ancestor
相关话题的讨论汇总
话题: treenode话题: root话题: null话题: node