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);
}
}
打印二叉树中某个节点的所有父节点
最新推荐文章于 2021-08-03 20:35:10 发布