Find the largest element in a binary tree java

Recursion

BinaryTreeNode findMax(BinaryTreeNode root){
    
    
  if(root == null){
    
    
    return null;
  }
  else{
    
    
    if(root.getRight() == null){
    
    
      return root;
    }
    else{
    
    
      return findMax(root.getRight());
    }
  }
}

Non-recursive

BinaryTreeNode findMax(BinaryTreeNode root){
    
    
  if(root == null){
    
    
    return null;
  }
  while(root.getRight() != null){
    
    
    root = root.getRight();
  }
  return root;
}

Guess you like

Origin blog.csdn.net/weixin_37632716/article/details/115218221