Maximize Nonadjacent Value Selection on a Binary Tree

Quick Overview

Maximize Nonadjacent Value Selection on a Binary Tree. Choose tree nodes maximizing their total value, with no directly linked parent and child both chosen. Return the maximum sum through the original TreeNode* root

Maximize Nonadjacent Value Selection on a Binary Tree

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Choose tree nodes maximizing their total value, with no directly linked parent and child both chosen. Return the maximum sum through the original TreeNode* root API. ```cpp int rob(TreeNode* root); ``` ### Examples ```text root = [4,2,5,3,null,null,1] -> 8 root = [0] -> 0 ``` ### Constraints - There are 1 through 10000 nodes. - Node values are in [0,10000].

Overview: Maximize Nonadjacent Value Selection on a Binary Tree. Choose tree nodes maximizing their total value, with no directly linked parent and child both chosen. Return the maximum sum through the original TreeNode* root

Amazon logo
Amazon
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Choose tree nodes maximizing their total value, with no directly linked parent and child both chosen. Return the maximum sum through the original TreeNode* root API.

int rob(TreeNode* root);

Examples

root = [4,2,5,3,null,null,1] -> 8
root = [0] -> 0

Constraints

  • There are 1 through 10000 nodes.
  • Node values are in [0,10000].