print all root to leaf paths in a binary tree

后端 未结 12 1389
野性不改
野性不改 2020-12-23 14:55

i am trying to print all root to leaf paths in a binary tree using java.

public void printAllRootToLeafPaths(Node node,ArrayList path) 
{
    if(node==null)         


        
12条回答
  •  抹茶落季
    2020-12-23 15:30

    public void PrintAllPossiblePath(Node node,List nodelist)
    {
        if(node != null)
        {
                nodelist.add(node);
                if(node.left != null)
                {
                    PrintAllPossiblePath(node.left,nodelist);
                }
                if(node.right != null)
                {
                    PrintAllPossiblePath(node.right,nodelist);
                }
                else if(node.left == null && node.right == null)
                {
    
                for(int i=0;i

    nodelist.remove(node) is the key, it removes the element once it prints the respective path

提交回复
热议问题