求根节点到x节点的路径问题

题目:求根结点到x结点的路径(假定结点不重复)

输入样例

输入一行字符序列先序递归构建二叉树。每个字符对应一个结点,#表示空结点。第二行输入一个结点值x。

52#3##41##6##
3

输出样例

5 2 3 

思路

节点类

class Node{
    char val;
    Node left;
    Node right;

    public Node(){
    }

}

第一步 首先先序创建一个二叉树

5 为根节点,2为5的左节点,#为空,即2的左节点为空,2的右节点为3,后面跟两个#说明3是一个叶子节点,回溯到根节点5,5的右节点为4,4的左节点为1,1是一个叶子节点,回溯到4,4的右节点为6.

代码实现

public Node createTree(){
        Node node;
        char ch=this.data.charAt(index);
        index++;
        if (ch=='#'){
            node=null;
        }else{
            node=new Node();
            node.val=ch;
            node.left=createTree();//创建子树
            node.right=createTree();
        }
        return node;
    }

第二步 用一个栈来装找到对应元素后的回溯路径,然后进行查找,这里采用中序查找。

中序查找的顺序是左 中 右 

代码实现

 public void find(char val){
        this.val=val;
        search(root);
        print();
    }
public boolean search(Node node){
        boolean flag=false;

        if (node.left!=null)
            flag=search(node.left);

        flag=flag||node.val==val;

        if (flag){
            stack.push(node.val);
            return true;
        }

        if (node.right!=null)
            flag=search(node.right);

        if (flag)
            stack.push(node.val);

        return flag;
    }

完整代码

import java.util.Scanner;
import java.util.Stack;

public class test2 {
    static Scanner input=new Scanner(System.in);
    public static void main(String[] args) {
        String str=input.nextLine();
        String str2=input.nextLine();
        NewTree tree=new NewTree(str);
        tree.find(str2.charAt(0));
    }
}
class NewTree{

    Node root;
    String data;
    int index;
    char val;
    Stack<Character> stack=new Stack<>();

    public NewTree(String data){
        this.data=data;
        root=createTree();
    }
    public void find(char val){
        this.val=val;
        search(root);
        print();
    }
    public void print(){
        while (stack.size()>0)
        {
            System.out.print(stack.pop()+" ");
        }
    }
    public boolean search(Node node){
        boolean flag=false;

        if (node.left!=null)
            flag=search(node.left);

        flag=flag||node.val==val;

        if (flag){
            stack.push(node.val);
            return true;
        }

        if (node.right!=null)
            flag=search(node.right);

        if (flag)
            stack.push(node.val);

        return flag;
    }

    public Node createTree(){
        Node node;
        char ch=this.data.charAt(index);
        index++;
        if (ch=='#'){
            node=null;
        }else{
            node=new Node();
            node.val=ch;
            node.left=createTree();//创建子树
            node.right=createTree();
        }
        return node;
    }
}
class Node{
    char val;
    Node left;
    Node right;

    public Node(){
    }

}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值