您的位置:首页 > 其它

打印二叉树中某个节点的所有父节点

2014-06-30 20:55 288 查看
package tree;

public class PrintAncestorsofagivenode {

/**
* 打印二叉树中某个节点的所有父节点
* @param args
*/
public static boolean printan(TreeNode root,int num){
if(root==null) return false;
if(root.value==num) return true;
if(printan(root.left, num)||printan(root.right, num)){
System.out.print(root.value+" ");
return true;
}
return false;
}
public static void main(String[] args) {

TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.left.left.left = new TreeNode(7);
printan(root, 5);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐