avatar
Update10.0.1?# PDA - 掌中宝
S*C
1
Given Tree and Node n and int k, print all node which are at physical
distance <=k from n
这道题怎么做?
我可以打印离root距离 <= k的node
但不能打印离任何node距离 <= k的node
avatar
w*q
2
For real?
avatar
S*C
3
import java.util.Stack;
/*
* Given Tree and Node n and int k, print all node which are at physical
distance <=k from n
* @Amazon intern
*/
public class Solution {
public static void main(String args[]){
Solution solution = new Solution();
TreeNode a = new TreeNode(8);
TreeNode b = new TreeNode(6);
TreeNode c = new TreeNode(10);
TreeNode d = new TreeNode(9);
TreeNode e = new TreeNode(12);
TreeNode f = new TreeNode(4);
TreeNode g = new TreeNode(7);
TreeNode h = new TreeNode(3);
TreeNode i = new TreeNode(5);
a.left = b;
a.right = c;
c.left = d;
c.right = e;
b.left = f;
b.right = g;
f.left = h;
f.right = i;
solution.displayTree(a);
for (int k = 0; k < 13; k++) {
System.out.println(k + " closest nodes to root = 8 include: ");
solution.printkdistanceNodeDown(a, k);
System.out.println(k + " closest nodes to c = 10 include: ");
solution.printkdistanceNodeDown(a, c, k);
System.out.println(k + " closest nodes to i = 5 include: ");
solution.printkdistanceNodeDown(a, i, k);
System.out.println("--------------------------");
}
}

/* Recursive function to print all the nodes at distance k in the
tree (or subtree) rooted with given root. See */
public void printkdistanceNodeDown(TreeNode root, int k) {
// Base Case
if (root == null || k < 0)
return;
// If we reach a k distant node, print it
if (k >= 0)
System.out.println(root.val);
// Recur for left and right subtrees
printkdistanceNodeDown(root.left, k - 1);
printkdistanceNodeDown(root.right, k - 1);
}
// Prints all nodes at distance k from a given target node.
// The k distant nodes may be upward or downward. This function
// Returns distance of root from target node, it returns -1 if target
// node is not present in tree rooted with root.
public int printkdistanceNodeDown(TreeNode root, TreeNode target, int k)
{
// Base Case 1: If tree is empty, return -1
if (root == null)
return -1;
// If target is same as root. Use the downward function
// to print all nodes at distance k in subtree rooted with
// target or root
if (root == target) {
printkdistanceNodeDown(root, k);
return 0;
}
// Recur for left subtree
int dl = printkdistanceNodeDown(root.left, target, k);
// Check if target node was found in left subtree
if (dl != -1) {
// If root is at distance k from target, print root
// Note that dl is Distance of root's left child from target
if (dl + 1 <= k)
System.out.println(root.val);
// Else go to right subtree and print all k-dl-2 distant nodes
// Note that the right child is 2 edges away from left child
else
printkdistanceNodeDown(root.right, k - dl - 2);
// Add 1 to the distance and return value for parent calls
return 1 + dl;
}
// MIRROR OF ABOVE CODE FOR RIGHT SUBTREE
// Note that we reach here only when node was not found in left
subtree
int dr = printkdistanceNodeDown(root.right, target, k);
if (dr != -1) {
if (dr + 1 <= k)
System.out.println(root.val);
else
printkdistanceNodeDown(root.left, k - dr - 2);
return 1 + dr;
}
// If target was neither present in left nor in right subtree
return -1;
}




public TreeNode sortedArrayToBST(int num[]) {
if(num.length==0) return null;
return rec(num, 0, num.length - 1);
}
private TreeNode rec(int num[], int start, int end) {
if (start > end)
return null;
int mid = (start + end) / 2;
TreeNode node = new TreeNode(num[mid]);
node.left = rec(num, start, mid - 1);
node.right = rec(num, mid + 1, end);
return node;
}
public void displayTree(TreeNode node) {
Stack globalStack = new Stack();
globalStack.push(node);
int nBlanks = 40;
boolean isRowEmpty = false;
System.out
.println("\n................................................
......");
while (isRowEmpty == false) {
Stack localStack = new Stack();
isRowEmpty = true;
for (int j = 0; j < nBlanks; j++)
System.out.print(' ');
while (globalStack.isEmpty() == false) {
TreeNode temp = (TreeNode) globalStack.pop();
if (temp != null) {
System.out.print(temp.val);
localStack.push(temp.left);
localStack.push(temp.right);
if (temp.left != null || temp.right != null)
isRowEmpty = false;
} else {
System.out.print("--");
localStack.push(null);
localStack.push(null);
}
for (int j = 0; j < nBlanks * 2 - 2; j++)
System.out.print(' ');
} // end while globalStack not empty
System.out.println();
nBlanks /= 2;
while (localStack.isEmpty() == false)
globalStack.push(localStack.pop());
} // end while isRowEmpty is false
System.out.println("................................................
......");
}
}
avatar
d*c
4
你的距离是说中间隔得node吗?如果是的话,bfs不就好了吗?找到level k。打印那个
level的。
avatar
n*n
5
肯定错了。这么长。

【在 S*******C 的大作中提到】
: import java.util.Stack;
: /*
: * Given Tree and Node n and int k, print all node which are at physical
: distance <=k from n
: * @Amazon intern
: */
: public class Solution {
: public static void main(String args[]){
: Solution solution = new Solution();
: TreeNode a = new TreeNode(8);

avatar
S*C
6
是中间隔的node,但是要求的是给定任意node距离最近的K个nodes
这个并不好写

【在 d*****c 的大作中提到】
: 你的距离是说中间隔得node吗?如果是的话,bfs不就好了吗?找到level k。打印那个
: level的。

avatar
x*9
7
分两种情况讨论:
1. 节点N是当前节点的儿子
2. 节点N一定不是当前节点的儿子
分着做好一些吧
avatar
S*C
9
我就是照着这个改的,但怎样改的有效率呢?
总不能写一个循环让i从0递增到k吧

【在 a*******3 的大作中提到】
: 这应该是geeksforgeeks上面一道题的变形,给个链接:
: http://www.geeksforgeeks.org/print-nodes-distance-k-given-node-
: 在这个链接里给的解法的基础上稍微改改就行了感觉

avatar
c*0
10
你改得不是挺好嘛。
if (k >= 0)
System.out.println(root.val);
if (dl + 1 <= k)
System.out.println(root.val);
还希望如何更有效率呢?
avatar
S*C
11
我这种写法输出来的东西是错的

【在 c*******0 的大作中提到】
: 你改得不是挺好嘛。
: if (k >= 0)
: System.out.println(root.val);
: if (dl + 1 <= k)
: System.out.println(root.val);
: 还希望如何更有效率呢?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。