Answers for "max depth of binary tree"

0

max depth of binary tree

public class TreeNode{

        TreeNode leftPointer,rightPointer;
        int data;
        public TreeNode(){
            leftPointer= null;
            rightPointer= null;
            data = 0;
        }
        public TreeNode(int n ){
            leftPointer=null;
            rightPointer=null;
            data=n;
        }

        public TreeNode getLeftPointer() {
            return leftPointer;
        }

        public void setLeftPointer(TreeNode leftPointer) {
            this.leftPointer = leftPointer;
        }

        public TreeNode getRightPointer() {
            return rightPointer;
        }

        public void setRightPointer(TreeNode rightPointer) {
            this.rightPointer = rightPointer;
        }

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }
        public int maxDepth(TreeNode root){
            if(root==null){return 0;}
            return 1+Math.max(maxDepth(root.leftPointer),maxDepth(root.rightPointer));
        }
Posted by: Guest on October-22-2021

Code answers related to "max depth of binary tree"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language