.Find max distance between any two nodes of a binary tree with at most a single bent.

  • 0

    We need to find the distance between two nodes in Binary Tree with one bent. Let L=> Left R=>Right .
    Bent actually denotes LLLLLLLRRRRR in this case there is only one bent , LLLRRRLLLL there is two bent,
    RRRRRLLLLL only one bent .

  • 0

    public class SingleBent {

    static class TreeNode   {
        int val;
        TreeNode left;
        TreeNode right;
    static int max = Integer.MIN_VALUE;
    public int bent(TreeNode root)  {
        return Math.max(max, bentUtil(root.left, false) + bentUtil(root.right, true));
    private int bentUtil(TreeNode root, boolean b) {
        if (root == null)
            return 0;
        int left = bentUtil(root.left, false);
        int right = bentUtil(root.right, true);
        max = Math.max(max, left + right );
        return b ? right : left;



Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.