Monday, December 14, 2015

get Root to given Node path in a Binary Tree

    public void rootToNode(int data)    {
        getRootToNodePath(this, data, new StringBuffer(), false);
    }
    private void getRootToNodePath(BinarySearchTree T, int data, StringBuffer sb, boolean isFound)    {
        if(T!=null && !isFound)    {
            sb.append(T.data).append(", ");
            if(T.data==data)    {
                System.out.println(sb.toString());
                isFound = true;
                return;
            }    else    {
                getRootToNodePath(T.left, data, new StringBuffer(sb), isFound);
                getRootToNodePath(T.right, data, new StringBuffer(sb), isFound);
            }
        }

    }

No comments:

Post a Comment

UA-39217154-2